package com.dycong.common.IO.netty.Packet_Splicing;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Created by dycong on 2017/3/13.
 */
public class EchoClientHandler extends ChannelInboundHandlerAdapter {

    static int count = 0;

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        this.channel = ctx;
        ctx.writeAndFlush("Netty rocks!"+(++count));
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx,
                               Object msg) throws Exception {
        System.out.println("ClientReader received: " + msg.toString());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,
                                Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    ChannelHandlerContext channel;

    public boolean sendMsg(String msg) {
        String i="com.dycong.common.IO.nio data transfer is based on buffers (java.nio.Buffer and related classes). These classes represent a contiguous extent of memory, together with a small number of data transfer operations. Although theoretically these are general-purpose data structures, the implementation may select memory for alignment or paging characteristics, which are not otherwise accessible in Java. Typically, this would be used to allow the buffer contents to occupy the same physical memory used by the underlying operating system for its native I/O operations, thus allowing the most direct transfer mechanism, and eliminating the need for any additional copying. In most operating systems, provided the particular area of memory has the right properties, transfer can take place without using the CPU at all. The com.dycong.common.IO.nio buffer is intentionally limited in features in order to support these goals.\n" +
                "\n" +
                "There are buffer classes for all of Java's primitive types except boolean, which can share memory with byte buffers and allow arbitrary interpretation of the underlying bytes.\n" +
                "\n" +
                "Usage[edit]\n" +
                "com.dycong.common.IO.nio buffers maintain several pointers that dictate the function of their accessor methods. The com.dycong.common.IO.nio buffer implementation contains a rich set of methods for modifying these pointers:\n" +
                "\n" +
                "The flip() method, rather than performing a \"flip\" or paging function in the canonical sense, moves the position pointer to the origin of the underlying array (if any) and the limit pointer to the former position of the position pointer.\n" +
                "Three get() methods are supplied for transferring data out of a com.dycong.common.IO.nio buffer. The bulk implementation, rather than performing a \"get\" in the traditional sense, \"puts\" the data into a specified array. The \"offset\" argument supplied to this method refers not to the offset from within the buffer from which to read, nor an offset from the position pointer, but rather the offset from 0 within the target array.\n" +
                "Unless using the absolute get() and put() methods, any get() or put() is conducted from the position pointer. Should one need to read from a different position within the underlying array, whilst not adjusting the writing position, the mark() and reset() methods have been supplied.\n" +
                "The mark() method effectively stores the position of the position pointer by setting the mark pointer to the position of the position pointer. The reset() method causes the position pointer to move to the mark pointer's position.\n" +
                "Upon invocation of the clear() method or the flip() method the mark pointer is discarded.\n" +
                "The clear() method does not ensure zero-ing of the buffer, but does return the limit pointer to the upper boundary of the underlying array, and the position pointer to zero.\n" +
                "put() and get() operations for com.dycong.common.IO.nio buffers are not thread safe.\n" +
                "You can only map() a java.nio.MappedByteBuffer from a java.nio.channels.FileChannel up to Integer.MAX_VALUE in size (2GiB); regions beyond this limit can be accessed using an offset greater than zero.\n" +
                "Channels[edit]\n" +
                "\n" +
                "File channels (java.nio.channels.FileChannel) can use arbitrary buffers but can also establish a buffer directly mapped to file contents using memory-mapped file. They can also interact with file system locks. Similarly, socket channels (java.nio.channels.SocketChannel and java.nio.channels.ServerSocketChannel) allow for data transfer between sockets and com.dycong.common.IO.nio buffers.\n" +
                "\n" +
                "FileChannel can be used to do a file copy, which is potentially far more efficient than using old read/write with a byte array. The typical code for this is:";
        String ii="123";
        System.out.println("客户端发送消息：" + msg);
        for (int t=0;t<5;t++)
            i+=i;
//        while (true)

        channel.writeAndFlush(i);
        return msg.equals("q") ? false : true;
    }
}
